package com.marakana;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class CameraActivity extends Activity {
	private static final String TAG = "CameraDemo";
	Preview preview; // <1>
	Button buttonClick; // <2>
	Button buttonStream;
	EditText edittext;
	ImageView imgView;

	private static ServerSocket serverSocket;
	Thread serverThread = null;
	public static final int SERVERPORT = 6000;
	OutputStream mOutputStream;
	InputStream mInputStream;
	DataOutputStream mPrintOut;
	PrintWriter mPrintWriter;
	BufferedReader mInputBuffer;
	boolean mIsStreamingDone = true;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		preview = new Preview(this); // <3>
		preview.mMainActivity = this;
		((FrameLayout) findViewById(R.id.preview)).addView(preview); // <4>

		// buttonClick = (Button) findViewById(R.id.buttonClick);
		// buttonClick.setOnClickListener(new OnClickListener() {
		// public void onClick(View v) { // <5>
		// preview.camera.takePicture(shutterCallback, rawCallback,
		// jpegCallback);
		// }
		// });
		this.serverThread = new Thread(new ServerThread());
		this.serverThread.start();
		Log.d(TAG, "onCreate'd");
	}

	class ServerThread implements Runnable {
		@Override
		public void run() {
			Socket clientSocket = null;
			try {
				serverSocket = new ServerSocket(SERVERPORT);
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				Log.e("tag", "Waiting client...");
				clientSocket = serverSocket.accept();
				Log.e("tag", "Accept client...");
				if (clientSocket != null) {
					mOutputStream = clientSocket.getOutputStream();
					mInputStream = clientSocket.getInputStream();
					mPrintWriter = new PrintWriter(new BufferedWriter(
							new OutputStreamWriter(mOutputStream)), true);
					mInputBuffer = new BufferedReader(new InputStreamReader(
							mInputStream));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	// Called when shutter is opened
	ShutterCallback shutterCallback = new ShutterCallback() { // <6>
		public void onShutter() {
			Log.d(TAG, "onShutter'd");
		}
	};

	public void getBuffer(byte[] data) {

		if (mOutputStream != null) {
			if (mIsStreamingDone) {
				try {
					mIsStreamingDone = false;
					// Send command streaming
					mPrintWriter.println("stream");
					// Send file size
					mPrintWriter.println(String.valueOf(data.length));
					// Read command back
					String command = mInputBuffer.readLine();
					// Send file
					mOutputStream.write(data);
					Log.e("tag",
							"Image send to client: "
									+ String.valueOf(data.length));
					// Wait command back
					command = mInputBuffer.readLine();
					if (command.equalsIgnoreCase("read_file_complete")) {
						Log.e("tag", "Streaming done");
						mIsStreamingDone = true;
						mOutputStream.flush();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	// Handles data for raw picture
	PictureCallback rawCallback = new PictureCallback() { // <7>
		public void onPictureTaken(byte[] data, Camera camera) {
			Log.d(TAG, "onPictureTaken - raw");
		}
	};

	// Handles data for jpeg picture
	PictureCallback jpegCallback = new PictureCallback() { // <8>
		public void onPictureTaken(byte[] data, Camera camera) {
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		mIsStreamingDone = false;
		mPrintWriter.println("off");
		try {
			mPrintOut.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mPrintWriter.close();
		super.onBackPressed();
	}

}
